﻿package mortal.game.manager
{
    import com.gengine.global.*;
    import com.gengine.ui.timer.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.mvc.core.*;

    public class ClockManager extends EventDispatcher
    {
        private var _gameClock:GameClock;
        public const DayTime:Number = 8.64e+007;
        private var _roleCreateDate:Date;
        private var _timeMap:Dictionary;
        private var _timeMapParams:Dictionary;
        private var _serverDate:Date;
        private var _serverOpenDate:Date;
        private var _serverOpenDateNum:int;
        private static var _instance:ClockManager;

        public function ClockManager()
        {
            this._timeMap = new Dictionary();
            this._timeMapParams = new Dictionary();
            if (_instance != null)
            {
                throw new Error("ClockManager 单例");
            }
            return;
        }// end function

        public function get serverOpenDateNum() : int
        {
            return this._serverOpenDateNum;
        }// end function

        public function set serverOpenDate(param1:Date) : void
        {
            this._serverOpenDate = param1;
            var _loc_2:* = new Date(param1.fullYear, param1.month, param1.date);
            this._serverOpenDateNum = Math.ceil((this.nowDate.time - _loc_2.time) / (1000 * 3600 * 24));
            this.serverOpenDayChange();
            return;
        }// end function

        public function set roleCreateDate(param1:Date) : void
        {
            this._roleCreateDate = param1;
            return;
        }// end function

        private function initGameClock() : void
        {
            this._gameClock = new GameClock();
            this._gameClock.enterFrameHandler = this.enterFrameHandler;
            this._gameClock.x = 100;
            this._gameClock.y = 90;
            return;
        }// end function

        public function addToStage() : void
        {
            if (!this._gameClock)
            {
                this.initGameClock();
            }
            if (this._gameClock.parent == null)
            {
                Global.stage.addChild(this._gameClock);
            }
            return;
        }// end function

        private function enterFrameHandler(param1:Date) : void
        {
            var _loc_2:Date = null;
            var _loc_3:Object = null;
            var _loc_4:Function = null;
            var _loc_5:Object = null;
            if (this._serverDate.date < param1.date || this._serverDate.month < param1.month || this._serverDate.fullYear < param1.fullYear)
            {
                this._serverDate.time = param1.time;
                dispatchEvent(new TimeEvent(TimeEvent.DateChange));
                var _loc_6:String = this;
                var _loc_7:* = this._serverOpenDateNum + 1;
                _loc_6._serverOpenDateNum = _loc_7;
                this.serverOpenDayChange();
            }
            if (this._serverDate.hours == 0)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.HourChange));
            }
            for (_loc_3 in this._timeMap)
            {
                
                _loc_2 = _loc_3 as Date;
                if (_loc_2)
                {
                    if (_loc_2.time <= param1.time)
                    {
                        _loc_4 = this._timeMap[_loc_2] as Function;
                        _loc_5 = this._timeMapParams[_loc_2];
                        if (_loc_5 != null)
                        {
                            _loc_4.apply(null, [_loc_5]);
                        }
                        else
                        {
                            _loc_4.apply();
                        }
                        this.removeDateCall(_loc_2);
                    }
                }
            }
            return;
        }// end function

        public function addDateCall(param1:Date, param2:Function, param3:Object = null) : void
        {
            if (param2 is Function)
            {
                if (param1.time > this._gameClock.nowDate.time)
                {
                    this._timeMap[param1] = param2;
                    this._timeMapParams[param1] = param3;
                }
            }
            return;
        }// end function

        public function removeDateCall(param1:Date) : void
        {
            if (this._timeMap[param1])
            {
                this._timeMap[param1] = null;
                this._timeMapParams[param1] = null;
                delete this._timeMap[param1];
                delete this._timeMapParams[param1];
            }
            return;
        }// end function

        private function serverOpenDayChange() : void
        {
            dispatchEvent(new TimeEvent(TimeEvent.ServerOpenDayChange));
            return;
        }// end function

        public function setNowTimer(param1:Object) : void
        {
            if (this._gameClock == null)
            {
                this._gameClock = new GameClock();
                this._gameClock.enterFrameHandler = this.enterFrameHandler;
                this._gameClock.x = 100;
                this._gameClock.y = 90;
            }
            this._gameClock.time = param1;
            this._serverDate = new Date(this._gameClock.nowDate.time);
            return;
        }// end function

        public function resetNowTimer(param1:Date) : void
        {
            if (this._gameClock)
            {
                this._gameClock.time = param1;
                this.enterFrameHandler(param1);
            }
            return;
        }// end function

        public function get nowDate() : Date
        {
            if (!this._gameClock)
            {
                this.initGameClock();
            }
            return this._gameClock.nowDate;
        }// end function

        public function getDelayDate(param1:Number) : Date
        {
            var _loc_2:* = this.nowDate.time + param1;
            return new Date(_loc_2);
        }// end function

        public function getLoginDayNumByPassZero() : int
        {
            var _loc_1:uint = 0;
            var _loc_2:uint = 0;
            var _loc_3:int = 0;
            if (this._roleCreateDate && this._gameClock.nowDate)
            {
                _loc_1 = int(this._gameClock.nowDate.time / this.DayTime);
                _loc_2 = int(this._roleCreateDate.time / this.DayTime);
                _loc_3 = _loc_1 - _loc_2 + 1;
                return _loc_3;
            }
            return 1;
        }// end function

        public function isNotCreateRoleDay() : Boolean
        {
            if (this._roleCreateDate && this._gameClock.nowDate)
            {
                return this._roleCreateDate.date != this._gameClock.nowDate.date || this._roleCreateDate.month != this._gameClock.nowDate.month || this._roleCreateDate.fullYear != this._gameClock.nowDate.fullYear;
            }
            return false;
        }// end function

        public function get todayLeftMillisecond() : int
        {
            return this.DayTime - (this.nowDate.hours * 60 * 60 * 1000 + this.nowDate.minutes * 60 * 1000 + this.nowDate.seconds * 1000 + this.nowDate.milliseconds);
        }// end function

        public function getRunningTime() : Number
        {
            return this._gameClock.nowDate.time - this._serverDate.time;
        }// end function

        public function get serverOpenDate() : Date
        {
            return this._serverOpenDate;
        }// end function

        public function isWithinTheTime(param1:int, param2:int, param3:int, param4:int, param5:int, param6:int, ... args) : Boolean
        {
            args = false;
            var _loc_13:int = 0;
            var _loc_9:* = this.nowDate;
            var _loc_10:* = new Date(_loc_9.fullYear, _loc_9.month, _loc_9.date, param1, param2, param3);
            var _loc_11:* = new Date(_loc_9.fullYear, _loc_9.month, _loc_9.date, param4, param5, param6);
            var _loc_12:* = this.nowDate.day;
            if (_loc_9.time > _loc_10.time && _loc_9.time <= _loc_11.time)
            {
                if (args.length)
                {
                    for each (_loc_13 in args)
                    {
                        
                        if (_loc_12 == _loc_13)
                        {
                            args = true;
                            break;
                        }
                    }
                }
                else
                {
                    args = true;
                }
            }
            return args;
        }// end function

        public static function get instance() : ClockManager
        {
            if (_instance == null)
            {
                _instance = new ClockManager;
            }
            return _instance;
        }// end function

    }
}
